package com.corx.network.mysqlstorage.repository;

import com.corx.network.mysqlstorage.common.JdbcPagingSupport;
import com.corx.network.mysqlstorage.common.PageQuery;
import com.corx.network.mysqlstorage.common.PageResult;
import com.corx.network.mysqlstorage.common.SqlQueryBuilder;
import com.corx.network.mysqlstorage.model.NetworkReceivedMessageListenerJar;
import org.springframework.dao.support.DataAccessUtils;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;

import java.util.List;
import java.util.Optional;

/**
 * @author gx
 * @version 1.0.0
 */
public class JdbcNetworkReceivedMessageListenerJarRepository extends JdbcPagingSupport<NetworkReceivedMessageListenerJar> implements NetworkReceivedMessageListenerJarRepository {


    public JdbcNetworkReceivedMessageListenerJarRepository(JdbcTemplate jdbcTemplate) {
        super(jdbcTemplate);
    }

    @Override
    protected RowMapper<NetworkReceivedMessageListenerJar> getRowMapper() {
        return (rs, rowNum) -> {
            NetworkReceivedMessageListenerJar data = new NetworkReceivedMessageListenerJar();
            data.setId(rs.getLong("id"));
            data.setNetworkId(rs.getLong("network_id"));
            data.setFilePath(rs.getString("file_path"));
            data.setStatus(rs.getString("status"));
            data.setDescription(rs.getString("description"));
            data.setCreateTime(rs.getTimestamp("create_time"));
            data.setUpdateTime(rs.getTimestamp("update_time"));
            return data;
        };
    }

    @Override
    public Optional<NetworkReceivedMessageListenerJar> findById(Long id) {
        List<NetworkReceivedMessageListenerJar> list = jdbcTemplate.query("SELECT * FROM corx_network_received_message_listener_jar WHERE id = ?", getRowMapper(), id);
        return Optional.ofNullable(DataAccessUtils.singleResult(list));
    }


    @Override
    public List<NetworkReceivedMessageListenerJar> findByNetworkId(Long networkId) {
        return jdbcTemplate.query("SELECT * FROM corx_network_received_message_listener_jar WHERE network_id = ?", getRowMapper(), networkId);
    }



    @Override
    public PageResult<NetworkReceivedMessageListenerJar> findByPage(NetworkReceivedMessageListenerJar model, PageQuery pageQuery) {
        SqlQueryBuilder queryBuilder = new SqlQueryBuilder("SELECT * FROM corx_network_received_message_listener_jar");
        if (model.getStatus() != null && !model.getStatus().isEmpty()) {
            queryBuilder.whereEquals("status", model.getStatus());
        }

        if (model.getNetworkId() != null) {
            queryBuilder.whereEquals("network_id", model.getNetworkId());
        }
        if (pageQuery.hasSort()) {
            queryBuilder.orderBy(pageQuery.orderBy());
        }
        String baseSql = queryBuilder.getSql();
        Object[] params = queryBuilder.getParams();
        return super.findByPage(baseSql, params, pageQuery);

    }

    @Override
    public void save(NetworkReceivedMessageListenerJar model) {
        String sql = "INSERT INTO corx_network_received_message_listener_jar (network_id, file_path, description, status, create_time, update_time) VALUES (?, ?, ?, ?, NOW(), NOW())";
        jdbcTemplate.update(sql, model.getNetworkId(), model.getFilePath(), model.getDescription(), model.getStatus());
    }

    @Override
    public void update(NetworkReceivedMessageListenerJar model) {
        String sql = "UPDATE corx_network_received_message_listener_jar SET network_id = ?, file_path = ?, description = ?, status = ?, update_time = NOW() WHERE id = ?";
        jdbcTemplate.update(sql, model.getNetworkId(), model.getFilePath(), model.getDescription(), model.getStatus(), model.getId());
    }

    @Override
    public void delete(Long id) {
        jdbcTemplate.update("DELETE FROM corx_network_received_message_listener_jar WHERE id = ?", id);
    }
}
